﻿using Shared.Http.APIResult;

namespace WebApi.AntiCorruptions
{
    public static class OperationResult
    {
        public static ApiResult<T> ToApiResult<T>(this Application.OperationResult<T> operationResult, Guid traceId)
        {
            if (operationResult.IsSuccess)
            {
                return ApiResult.Success(operationResult.Data!, traceId);
            }
            else
            {
                var errors = operationResult.Errors
                    .Select(e => new ErrorData(e.Id, e.Message))
                    .ToList();
                if (operationResult.Data is not null)
                {
                    return ApiResult.PartFail<T>( operationResult.Data, traceId, errors);
                }

                return ApiResult.Fail<T>(ApiStatusCode.BusinessError, traceId, errors);
            }
        }
    }
}
